<?php
#echo "slots=<pre>".print_r($slots,true)."</pre><br />\n";
#die();
echo $this->Html->css('menu', array('inline'=>false));
echo $this->Html->css('jquery-ui', array('inline'=>false));
#echo $this->Html->css('jquery-ui-1.11.2.custom/jquery-ui-min', array('inline'=>false));
echo $this->Html->script('jquery-1.11.1.min', array('inline'=>false));
echo $this->Html->script('jquery-ui-1.11.2.custom/jquery-ui', array('inline'=>false));
echo $this->Html->script('jquery.PrintArea', array('inline'=>false));
echo $this->Html->script('ptsignup', array('inline'=>false));
echo $this->Html->scriptStart();
?>
var userID = <?= $user_id ?>;
var schoolID = <?= $school_id ?>;
var deptID = <?= $dept_id ?>;
var familyID = <?= ($family_id ? $family_id : -1) ?>;
var curView = '<?= $curView ?>';
var urlSignupBase = '<?= Configure::read('ptsignup.urlSignupBase'); ?>';
<?php
echo $this->Html->scriptEnd();
$class_normal = array('class'=>'link2buttn', 'escape'=>false);
$class_highlight = $class_normal;
$class_highlight['class'] .= ' ui-state-highlight';
$header_html = '<nav id="primary_nav_wrap"><ul>';
if (sizeof($dept_list) > 1) {
    $dept_html = '<li>';
    $dept_details = '';
    if (sizeof($dept_list) > 1) {
        #echo "<br />\n";
        #echo "DEBUG: Dept: ($dept_id) ";
        foreach ($dept_list as $dept) {
            if ($dept['id'] == $dept_id) {
                $dept_html .= $this->Html->link('Dept: '.$dept['name'], array('dept'=>$dept['id'], 'view'=>$curView), array_merge($class_normal, array('style'=>'color:#003d4c'))).'<ul>';
                $myclass = $class_highlight;
            } else
                $myclass = $class_normal;
            $dept_details .= '<li>'.$this->Html->link($dept['name'], array('dept'=>$dept['id'], 'view'=>$curView), array_merge($myclass, array('style'=>'color:#003d4c'))).'</li>';
            #$header_html .= "\n";
        }
    }
    $header_html .= $dept_html.$dept_details.'</ul></li>';
}
if (sizeof($valid_views) > 1) {
    $header_html .= "<li>".$this->Html->link(__("View: ($curView)"), '#', $class_normal)."<ul>";
    foreach ($valid_views as $view) {
        if ($view == $curView) {
            $myclass = $class_highlight;
            $myclass['class'] .= ' current-menu';
        } else
            $myclass = $class_normal;
        $header_html .= '<li>'.$this->Html->link(ucfirst($view).' View', array('view'=>$view, 'dept'=>$dept_id), $myclass).'</li>';
        #echo "\n";
    }
    $header_html .= '</ul></li>';
}
$header_html .= '<li>'.$this->Html->link(__('Display Schedule'), '#', array_merge($class_normal, array('onclick'=>"showSchedule()"))).'</li></ul>';
$header_html .= '<ul style="float:right"><li>'.$this->Html->link(__('Log Out'), array('controller' => 'users', 'action' => 'logout'), array_merge($class_normal, array())).'</li>';
$header_html .= '</ul></nav>';
echo $this->Html->scriptStart();
?>
    $("#header").html('<?= $header_html ?>');
<?php
echo $this->Html->scriptEnd();

################################################
#
# Display the family name (list) option
#
################################################
$selectedFamilyID = $firstFamilyID = 0;
if ($curView == 'FAMILY') $invisible = 'display:none; ';
else $invisible = '';
$invisible = '';
echo "Current Family: <select id='family_id' name='family_id' onchange='familyChanged()' style='$invisible'>\n";
    #echo "DEBUG: families=<pre>".print_r($families,true)."</pre><br />\n";
foreach ($families as $family) {
    #echo "DEBUG: family=<pre>".print_r($family,true)."</pre><br />\n";
    if (!$firstFamilyID) $firstFamilyID = $family['id'];
    if (@$_REQUEST['family_id'] && $_REQUEST['family_id'] == $family['id']) {
        $selected='selected="selected"';
        $selectedFamilyID = $family['id'];
    } else
        $selected='';
    echo "<option $selected value='$family[id]'>$family[name]</option>\n";
}
echo "</select>\n<br /><br />\n";
if (!$selectedFamilyID)
    $selectedFamilyID = $firstFamilyID;
$last_date = $last_time = $real_last_time = $first_time = '';
$cols = 2; // left column and right column is time
$teacher_row = '<tr><th>&nbsp;</th>';
$out = '<table class="ui-widget-content" style="margin:auto; " >';
$jsarray = "\n".'var slots = [';
$reccount = 0;
foreach ($slots as $slot) {
    $reccount++;
    $jsarray .= "{ id:".$slot['id'].", family_id: ".($slot['family_id']?:0).", family_name:'".$slot['name']."'},\n";
    #echo "DEBUG: slot=<pre>".print_r($slot,true)."</pre>\n";
    if ($real_last_time != $slot['real_start_time']) {
        if ($last_time) $out .= "<td>$last_time</td></tr>";
        if ($last_date != $slot['start_date']) {
            $out .= "TEACHER_ROW\n";
            $out .= "<tr><th class='ui-widget-header' colspan='\$\$ColumnCount'>$slot[start_date]</th></tr>";
            $last_date = $slot['start_date'];
        }
        $out .= "<tr><td>$slot[start_time]</td>";
        $real_last_time = $slot['real_start_time'];
        $last_time = $slot['start_time'];
    }
    if (!$first_time) $first_time = $slot['real_start_time'];
    if ($first_time == $slot['real_start_time']) {
        $teacher_row .= "<th><span title='".$teacher_comments[$slot['teacher_id']]."'>$slot[display_name]</span></th>";
        $cols++;
    }
    $out .= "<td id='detail_$slot[id]' data-family-id='$slot[family_id]' align='center'>";
    if ($slot['family_id'])
        if ($slot['family_id'] == $selectedFamilyID)
            $out .= "<button class='ui-state-highlight' onclick='unsignup($slot[id])'>$slot[name]</button>";
        else
            $out .= "$slot[name]";
    else
        $out .= "<button onclick='signup($slot[id])'>Sign&nbsp;Up</button>";
    $out .= "</td>\n";
}
if ($reccount) {
    $out .= "<td>$last_time</td></tr>TEACHER_ROW</table>";
    $teacher_row .= "<th>&nbsp;</th></tr>";
    $out = str_replace(array('TEACHER_ROW', '$$ColumnCount'), array($teacher_row, $cols), $out);
    echo $out;
    $jsarray .= "];";
    echo "\n<script type='text/javascript'>\n$jsarray\n</script>\n";
} else {
    echo "You are not authorized to change any time slots.<br />\n";
}
?>
<div id="helpscreen" style="display:none">
    <i>Please wait. Loading content.</i>
</div>
<div id="schedule-div" style="display:none">
    <div id="schedule-div-errors" style="color:red;"></div>
</div>

